package com.xhui.vimarket.zkplus.utils;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.Clients;

/**
 * zk 客户端扩展.
 * 使用说明：使用本类需要界面引入：
 * <script type="text/javascript" src="/public/zul/zkjs.js" />
 * <script type="text/javascript" src="/public/zul/zkconst.js" />
 * 
 * @author wuyx
 * @version Revision 1.0.0
 * 
 */
public final class ClientUtil {
    
    /**
     * logger.
     */
    private static Logger      logger              = Logger.getLogger(ClientUtil.class);
    
    /**
     * OPEN_MOLD_MAX.
     */
    public static final String OPEN_MOLD_MAX       = "top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,status=no";
    
    /**
     * OPEN_MOLD_MAX.
     */
    public static final String OPEN_MOLD_MAX_NOSCR = "top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=yes,status=no";
    /**
     * TAR_PARENT.
     */
    public static final String TAR_PARENT          = "parent";
    /**
     * TAR_PARENT.
     */
    public static final String TAR_SELF            = "self";
    /**
     * TAR_TOP.
     */
    public static final String TAR_TOP             = "top";
    /**
     * TAR_SELF_TOP.
     */
    public static final String TAR_SELF_TOP        = "self,top";
    
    /**
     * 数据分隔符号.
     */
    public static final String TAG_SPLIT           = ":~~:";
    
    /**
     * 私有构造函数.
     */
    private ClientUtil() {
    }
    
    /**
     * open.
     * 
     * @param url String
     * @param idx String
     * @param param String
     * 使用例子：
     * open("index.zul","","top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,status=no");
     */
    public static void open(final String url, final String idx, final String param) {
        final StringBuffer jscript = new StringBuffer();
        final String urlen = Executions.encodeURL(url);
        jscript.append("zkj.f.open('");
        jscript.append(urlen);
        jscript.append("', '");
        jscript.append(idx);
        jscript.append("', '");
        jscript.append(param);
        jscript.append("');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * modal.
     * 
     * @param url String
     * @param objParam String
     */
    public static void modal(final String url, final String objParam) {
        ClientUtil.modal(url, objParam, ZkWindUtil.WIN_MID_SIZE);
    }
    
    /**
     * modal.
     * 
     * @param url String
     * @param objParam String
     * @param size int
     */
    public static void modal(final String url, final String objParam, final int size) {
        if (size == ZkWindUtil.WIN_MAX_SIZE) {
            ClientUtil.modal(url, objParam, ZkWindUtil.WIN_MAX_WIDTH, ZkWindUtil.WIN_MAX_HEIGHT);
        } else if (size == ZkWindUtil.WIN_MIN_SIZE) {
            ClientUtil.modal(url, objParam, ZkWindUtil.WIN_MIN_WIDTH, ZkWindUtil.WIN_MIN_HEIGHT);
        } else {
            ClientUtil.modal(url, objParam, ZkWindUtil.WIN_MID_WIDTH, ZkWindUtil.WIN_MID_HEIGHT);
        }
    }
    
    /**
     * modal.
     * 
     * @param url String
     * @param objParam String
     * @param w int
     * @param h int
     * "status:no;help:no;scroll:auto;dialogWidth:w;dialogHeight:h;"
     * 使用例子：
     * modal("index.zul","'123'", 100, 100);
     */
    public static void modal(final String url, final String objParam, final int w, final int h) {
        final StringBuffer jscript = new StringBuffer();
        final String urlen = Executions.encodeURL(url);
        jscript.append("zkj.f.modal('");
        jscript.append(urlen);
        jscript.append("', ");
        jscript.append(objParam);
        jscript.append(", '");
        jscript.append(w);
        jscript.append("px', '");
        jscript.append(h);
        jscript.append("px');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * modal.
     * 
     * @param url String
     * @param objParam String
     * @param w int
     * @param h int
     * @param features 属性
     * "status:no;help:no;scroll:auto;dialogWidth:w;dialogHeight:h;"
     * 使用例子：
     * modal("index.zul","'123'", 100, 100);
     */
    public static void modal(final String url, final String objParam, final int w, final int h,
        final String features) {
        final StringBuffer jscript = new StringBuffer();
        final String urlen = Executions.encodeURL(url);
        jscript.append("zkj.f.modal('");
        jscript.append(urlen);
        jscript.append("', ");
        jscript.append(objParam);
        jscript.append(", '");
        jscript.append(w);
        jscript.append("px', '");
        jscript.append(h);
        jscript.append("px', '");
        jscript.append(features);
        jscript.append("');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * close.
     * 
     * @param objParam String
     * 使用例子：
     * modal("'1'");
     */
    public static void close(final String objParam) {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("zkj.f.close(");
        jscript.append(objParam);
        jscript.append(");");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * close.
     * @param evtTar String
     * @param evtName String
     * @param evtStrData String
     * @param tarType 触发的目标：self,parent，默认：parent
     * 使用例子：
     * fireUiEvent("$btnid", "onClick", "''", "self");
     */
    public static void fireUiEvent(final String evtTar, final String evtName,
        final String evtStrData, final String tarType) {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("zkj.f.fireUiEvent('");
        jscript.append(evtTar);
        jscript.append("', '");
        jscript.append(evtName);
        jscript.append("', ");
        jscript.append(evtStrData);
        jscript.append(", '");
        jscript.append(tarType);
        jscript.append("');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * close.
     * @param evtTar String
     * @param evtName String
     * @param evtStrData String
     * @param tarType 触发的目标：self,parent
     * 使用例子：
     * fireUiEvent("$btnid", "onClick", "''", "self");
     */
    public static void fireUiTextEvent(final String evtTar, final String evtName,
        final String evtStrData, final String tarType) {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("zkj.f.fireUiTextEvent('");
        jscript.append(evtTar);
        jscript.append("', '");
        jscript.append(evtName);
        jscript.append("', ");
        jscript.append(evtStrData);
        jscript.append(", '");
        jscript.append(tarType);
        jscript.append("');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * 打开框架tab页面.
     * 
     * @param name String
     * @param path String
     * @author wuyx
     * 2011-5-13 wuyx 
     */
    public static void openTab(final String name, final String path) {
        String sdata = "'" + name + TAG_SPLIT + path + "'";
        ClientUtil.fireUiTextEvent("$txtOpenTab", "onClick", sdata, TAR_SELF_TOP);
    }
    
    /**
     * confirm.
     * @param message 提示信息
     * @param btnYes 确定后触发按钮
     * @param btnNo 取消后触发按钮
     * 使用例子：
     * fireUiEvent("确定?", "$btnYes", "$btnNo");
     */
    public static void confirm(final String message, final String btnYes, final String btnNo) {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("zkj.f.confirm('");
        jscript.append(message);
        jscript.append("', '");
        jscript.append(btnYes);
        jscript.append("', '");
        jscript.append(btnNo);
        jscript.append("');");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * 协议打印.
     * 
     * @author huangjb
     * 2011-5-4 huangjb 
     */
    public static void print() {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("print();");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
    
    /**
     * 协议打印预览.
     * 
     * @author huangjb
     * 2011-5-4 huangjb 
     */
    public static void preprint() {
        final StringBuffer jscript = new StringBuffer();
        jscript.append("preprint();");
        if (ClientUtil.logger.isDebugEnabled()) {
            ClientUtil.logger.debug(jscript.toString());
        }
        Clients.evalJavaScript(jscript.toString());
    }
}
